<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<table mat-table [dataSource]="dataSource"
  matSort [matSortActive]="pageLink.sortOrder?.property" [matSortDirection]="pageLink.sortDirection()" matSortDisableClear>
  <ng-container matColumnDef="select" sticky>
    <mat-header-cell *matHeaderCellDef style="width: 30px;">
      <mat-checkbox (change)="$event ? dataSource.masterToggle() : null"
        [checked]="dataSource.selection.hasValue() && (dataSource.isAllSelected() | async)"
        [indeterminate]="dataSource.selection.hasValue() && !(dataSource.isAllSelected() | async)">
      </mat-checkbox>
    </mat-header-cell>
    <mat-cell *matCellDef="let entity">
      <mat-checkbox (click)="$event.stopPropagation()"
        [class.!hidden]="!entitiesTableConfig().entitySelectionEnabled(entity)"
        (change)="$event ? dataSource.selection.toggle(entity) : null"
        [checked]="dataSource.selection.isSelected(entity)">
      </mat-checkbox>
    </mat-cell>
  </ng-container>
  @for (column of entityColumns; track column.key) {
    <ng-container [matColumnDef]="column.key" sticky>
      <mat-header-cell [class.mat-number-cell]="column.isNumberColumn"
        *matHeaderCellDef [style]="headerCellStyle(column)" mat-sort-header [disabled]="!column.sortable">
      {{ column.ignoreTranslate ? column.title : (column.title | translate) }} </mat-header-cell>
      <mat-cell [class.mat-number-cell]="column.isNumberColumn"
        *matCellDef="let entity; let row = index"
        [matTooltip]="cellTooltip(entity, column, row)"
        matTooltipPosition="above"
        [style]="cellStyle(entity, column, row)">
        <span [innerHTML]="cellContent(entity, column, row)"></span>
        @if (column.actionCell) {
          @switch (column.actionCell.type) {
            @case (cellActionType.COPY_BUTTON) {
              <tb-copy-button
                [disabled]="isLoading$ | async"
                [containerStyle]="{padding: '4px'}"
                [class.!hidden]="!column.actionCell.isEnabled(entity)"
                [copyText]="column.actionCell.onAction(null, entity)"
                tooltipText="{{ column.actionCell.nameFunction ? column.actionCell.nameFunction(entity) : column.actionCell.name }}"
                tooltipPosition="above"
                [icon]="column.actionCell.icon"
                [style]="column.actionCell.style">
              </tb-copy-button>
            }
            @default {
              <button mat-icon-button [disabled]="isLoading$ | async"
                [class.!hidden]="!column.actionCell.isEnabled(entity)"
                matTooltip="{{ column.actionCell.nameFunction ? column.actionCell.nameFunction(entity) : column.actionCell.name }}"
                matTooltipPosition="above"
                (click)="column.actionCell.onAction($event, entity)">
                <tb-icon [style]="column.actionCell.style">{{column.actionCell.icon}}</tb-icon>
              </button>
            }
          }
        }
      </mat-cell>
    </ng-container>
  }
  @for (column of actionColumns; track column.key) {
    <ng-container [matColumnDef]="column.key" sticky>
      <mat-header-cell *matHeaderCellDef [style]="headerCellStyle(column)" mat-sort-header [disabled]="!column.sortable"> {{ column.title | translate }} </mat-header-cell>
      <mat-cell *matCellDef="let entity; let row = index"
        [style]="cellStyle(entity, column, row)">
        <button mat-icon-button [disabled]="isLoading$ | async"
          [class.!hidden]="!column.actionDescriptor.isEnabled(entity)"
          matTooltip="{{ column.actionDescriptor.nameFunction ? column.actionDescriptor.nameFunction(entity) : column.actionDescriptor.name }}"
          matTooltipPosition="above"
          (click)="column.actionDescriptor.onAction($event, entity)">
          <tb-icon [style]="column.actionDescriptor.style">{{column.actionDescriptor.icon}}</tb-icon>
        </button>
      </mat-cell>
    </ng-container>
  }
  <ng-container matColumnDef="actions" stickyEnd>
    <mat-header-cell *matHeaderCellDef>
      @if (cellActionDescriptors.length) {
        <div class="gt-md:!hidden" style="min-width: 48px;">
          {{ entitiesTableConfig().actionsColumnTitle ? (entitiesTableConfig().actionsColumnTitle | translate) : '' }}
        </div>
        <div class="lt-lg:!hidden"
             [style.min-width]="(cellActionDescriptors.length * 48) + 'px'">
          {{ entitiesTableConfig().actionsColumnTitle ? (entitiesTableConfig().actionsColumnTitle | translate) : '' }}
        </div>
      }
    </mat-header-cell>
    <mat-cell *matCellDef="let entity">
      <div [class.lt-lg:!hidden]="cellActionDescriptors.length !== 1" class="flex flex-row items-stretch justify-end"
           [style.min-width]="(cellActionDescriptors.length * 48) + 'px'">
        @for (actionDescriptor of cellActionDescriptors; track actionDescriptor) {
          <div matTooltipPosition="above"
            matTooltip="{{ actionDescriptor.nameFunction ? actionDescriptor.nameFunction(entity) : actionDescriptor.name }}">
            <button mat-icon-button [disabled]="(isLoading$ | async) || !actionDescriptor.isEnabled(entity)"
              (click)="actionDescriptor.onAction($event, entity)">
              <tb-icon [style]="actionDescriptor.style">{{actionDescriptor.icon}}</tb-icon>
            </button>
          </div>
        }
      </div>
      @if (cellActionDescriptors.length) {
        <div [class.!hidden]="cellActionDescriptors.length === 1" class="gt-md:!hidden">
          <button mat-icon-button
            (click)="$event.stopPropagation()"
            [matMenuTriggerFor]="cellActionsMenu">
            <mat-icon class="material-icons">more_vert</mat-icon>
          </button>
          <mat-menu #cellActionsMenu="matMenu" xPosition="before">
            @for (actionDescriptor of cellActionDescriptors; track actionDescriptor) {
              <button mat-menu-item
                [disabled]="isLoading$ | async"
                [class.!hidden]="!actionDescriptor.isEnabled(entity)"
                (click)="actionDescriptor.onAction($event, entity)">
                <tb-icon [style]="actionDescriptor.style">{{actionDescriptor.icon}}</tb-icon>
                <span>{{ actionDescriptor.nameFunction ? actionDescriptor.nameFunction(entity) : actionDescriptor.name }}</span>
              </button>
            }
          </mat-menu>
        </div>
      }
    </mat-cell>
  </ng-container>
  <mat-header-row [class.!hidden]="dataSource.isEmpty() | async"
                  [class.mat-row-select]="selectionEnabled"
                  *matHeaderRowDef="displayedColumns; sticky: true"></mat-header-row>
  <mat-row [class.!hidden]="dataSource.dataLoading"
           [class.mat-row-select]="selectionEnabled"
           [class.mat-selected]="dataSource.selection.isSelected(entity)"
           [class.tb-current-entity]="dataSource.isCurrentEntity(entity)"
           *matRowDef="let entity; columns: displayedColumns;" (click)="onRowClick($event, entity)"></mat-row>
</table>

@if (!(isLoading$ | async) && (dataSource.isEmpty() | async) && !dataSource.dataLoading) {
  <ng-container *ngTemplateOutlet="isEmpty"></ng-container>
} @else {
  <span class="flex flex-1"></span>
}

@if (displayPagination) {
  <mat-paginator
    [length]="dataSource.total() | async"
    [pageIndex]="pageLink.page"
    [pageSize]="pageLink.pageSize"
    [pageSizeOptions]="pageSizeOptions"
    [hidePageSize]="hidePageSize"
    showFirstLastButtons>
  </mat-paginator>
}

<ng-template #isEmpty>
  <span class="no-data-found flex flex-row items-center justify-center" translate>{{translations.noEntities}}</span>
</ng-template>

